Skip to content

test: unit tests for LocalLoginComponent#5235

Open
Ma77Ball wants to merge 5 commits into
apache:mainfrom
Ma77Ball:test/localLoginSpec
Open

test: unit tests for LocalLoginComponent#5235
Ma77Ball wants to merge 5 commits into
apache:mainfrom
Ma77Ball:test/localLoginSpec

Conversation

@Ma77Ball
Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

  • Add local-login.component.spec.ts to cover form construction, validators, default-user prefill, and the login/register flows that were previously untested.
  • Tests assert that allForms exposes the five expected controls with required, minLength(6), and the custom confirmationValidator, that confirmationValidator returns { confirm: true } on mismatch and {} on match, and that updateConfirmValidator schedules updateValueAndValidity on the confirmation control via setTimeout.
  • Tests assert that ngOnInit patches loginUsername/loginPassword only when GuiConfigService.env.defaultLocalUser is populated, that login short-circuits via loginErrorMessage on validation failure and otherwise calls UserService.login with the trimmed username and navigates to queryParams.returnUrl or DASHBOARD_USER_WORKFLOW, and that error paths surface the error's message (or the fallback "Incorrect username or password") through NotificationService.error.
  • Tests assert that register enforces password length, password match, and UserService.validateUsername, calls UserService.register with the trimmed username on success and surfaces the account-created notification, and on error notifies with the error's message (or the fallback "Registration failed").

Any related issues, documentation, or discussions?

Closes: #5226

How was this PR tested?

  • yarn test --include="src/app/hub/component/about/local-login/local-login.component.spec.ts", 22 tests passing.
  • yarn format:fix, 506 files unchanged.

Was this PR authored or co-authored using generative AI tooling?

Co-authored with Claude Opus 4.7 in compliance with ASF

@github-actions github-actions Bot added the frontend Changes related to the frontend GUI label May 26, 2026
@Ma77Ball
Copy link
Copy Markdown
Contributor Author

/request-review @Yicong-Huang

@github-actions github-actions Bot requested a review from Yicong-Huang May 26, 2026 21:46
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 46.18%. Comparing base (ca829ec) to head (e6faa2d).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5235      +/-   ##
============================================
- Coverage     49.11%   46.18%   -2.93%     
+ Complexity     2378     2217     -161     
============================================
  Files          1051     1052       +1     
  Lines         40342    40734     +392     
  Branches       4277     4340      +63     
============================================
- Hits          19812    18813     -999     
- Misses        19373    20779    +1406     
+ Partials       1157     1142      -15     
Flag Coverage Δ *Carryforward flag
access-control-service 39.53% <ø> (ø) Carriedforward from afb29e7
agent-service 33.76% <ø> (ø) Carriedforward from afb29e7
amber 44.22% <ø> (-7.35%) ⬇️ Carriedforward from afb29e7
computing-unit-managing-service 0.00% <ø> (ø) Carriedforward from afb29e7
config-service 0.00% <ø> (ø) Carriedforward from afb29e7
file-service 32.09% <ø> (-5.90%) ⬇️ Carriedforward from afb29e7
frontend 41.27% <ø> (+0.21%) ⬆️
python 90.50% <ø> (-0.30%) ⬇️ Carriedforward from afb29e7
workflow-compiling-service 56.81% <ø> (ø) Carriedforward from afb29e7

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chenlica chenlica requested a review from Xiao-zhen-Liu May 28, 2026 00:06
@chenlica
Copy link
Copy Markdown
Contributor

@Xiao-zhen-Liu Please review this PR.

Copy link
Copy Markdown
Contributor

@Xiao-zhen-Liu Xiao-zhen-Liu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is a clean, well-scoped addition — thanks for the thorough coverage. A few notes:

Strengths

  • Good decomposition: one component spec per PR fits the ongoing test-backfill series and keeps the review small (single new file, no production changes).
  • Very readable: clear describe/it grouping, descriptive names, and the createComponent(queryParams) helper with resetTestingModule() cleanly handles the returnUrl variant.
  • The 22 cases faithfully mirror the component's behavior (validators, default-user prefill, trimming, and the success/error/fallback paths for both login and register).

Main thing to address

  • The try/catch around component.login() / component.register() in the four error-path tests is dead code, and the accompanying comment is inaccurate — see the inline note. component.login() does not throw synchronously; the synchronous notificationService.error(...) assertions already cover the behavior, and the re-thrown error is reported asynchronously by RxJS rather than surfaced to the caller.

Minor / optional

  • Prefer mockReturnValueOnce on the already-created spy over (mock.x as any) = vi.fn() (inline note).
  • Coverage gaps worth a line each if you want completeness: the this.allForms && ... guard branch in confirmationValidator (the falsy-allForms path) is untested, and register() calls UserService.validateUsername before the password length/match checks — no test pins that ordering, so a future reorder wouldn't be caught.
  • beforeEach calls createComponent() and the returnUrl test calls it again (each with resetTestingModule); harmless but slightly wasteful.

None of these block; the only real cleanup is the dead try/catch.

Comment on lines +245 to +249
try {
component.login();
} catch {
// The component re-throws the error after notifying; swallow it here.
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/catch is dead code, and the comment is inaccurate. component.login() does not throw synchronously: on error the chain hits catchError, which calls notificationService.error(...) synchronously and then returns throwError(() => e). Because the component's .subscribe() has no error callback, RxJS does not surface that re-thrown error to the caller — it reports it asynchronously via hostReportError (a setTimeout), so there is nothing here for try/catch to catch. The notificationService.error assertion just below is what actually validates the behavior.

Suggest dropping the try/catch and the comment entirely. (If you want to be defensive about the leaked async error, the cleaner fix is to give the component's .subscribe() an explicit error handler rather than relying on the global unhandled-error path.)

Same pattern recurs at L260, L343, and L362.


it("surfaces the error's message via NotificationService.error on failure", () => {
vi.spyOn(UserService, "validateUsername").mockReturnValue({ result: true, message: "ok" });
(userServiceMock.login as any) = vi.fn().mockReturnValue(throwError(() => new Error("boom")));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reassigning the mock through as any after it was already created in createComponent works but reads awkwardly and drops the typing. Since login is already a vi.fn(), prefer overriding the queued return on the existing spy:

vi.mocked(userServiceMock.login!).mockReturnValueOnce(throwError(() => new Error("boom")));

Keeps the type and avoids swapping the function reference. Same at L257, L336, L355.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Changes related to the frontend GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add spec coverage for local-login.component.ts

4 participants